package com.joker.aigc.chat.domain.enums;


import com.joker.aigc.chat.domain.entity.websocket.WebSocketBlackVo;
import com.joker.aigc.chat.domain.entity.websocket.WebSocketFriendApplyVo;
import com.joker.aigc.chat.domain.entity.websocket.WebSocketInvitationVo;
import com.joker.aigc.chat.domain.entity.websocket.WebSocketLoginSuccessVo;
import com.joker.aigc.chat.domain.entity.websocket.WebSocketLoginUrlVo;
import com.joker.aigc.chat.domain.entity.websocket.WebSocketMemberChangeVo;
import com.joker.aigc.chat.domain.entity.websocket.WebSocketMsgMarkVo;
import com.joker.aigc.chat.domain.entity.websocket.WebSocketMsgRecallVo;
import com.joker.aigc.chat.domain.entity.websocket.WebSocketNotificationVo;
import com.joker.aigc.chat.domain.entity.websocket.WebSocketOnlineOfflineNotifyVo;
import com.joker.aigc.chat.domain.vo.MessageVo;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Description: ws前端请求类型枚举
 * Author: <a href="https://github.com/zongzibinbin">abin</a>
 * Date: 2023-03-19
 */
@AllArgsConstructor
@Getter
public enum WebSocketVoTypeEnum {

    LOGIN_URL(1, "登录二维码返回", WebSocketLoginUrlVo.class, "login_url"),
    LOGIN_SCAN_SUCCESS(2, "用户扫描成功等待授权", null, "login_scan_success"),
    LOGIN_SUCCESS(3, "用户登录成功返回用户信息", WebSocketLoginSuccessVo.class, "login_success"),
    MESSAGE(4, "新消息", MessageVo.class, "message"),
    ONLINE_OFFLINE_NOTIFY(5, "上下线通知", WebSocketOnlineOfflineNotifyVo.class, "online_offline"),
    INVALIDATE_TOKEN(6, "使前端的token失效，意味着前端需要重新登录", null, "invalidate_token"),
    BLACK(7, "拉黑用户", WebSocketBlackVo.class, "black"),
    MARK(8, "消息标记", WebSocketMsgMarkVo.class, "mark"),
    RECALL(9, "消息撤回", WebSocketMsgRecallVo.class, "recall"),
    APPLY(10, "好友申请", WebSocketFriendApplyVo.class, "apply"),
    MEMBER_CHANGE(11, "成员变动", WebSocketMemberChangeVo.class, "member_change"),
    ROOM_INVITATION(12, "邀请新成员", WebSocketInvitationVo.class, "room_invitation"),
    NOTIFICATION(13, "通知", WebSocketNotificationVo.class, "notification");

    private final Integer type;
    private final String desc;
    private final Class dataClass;
    private final String routerKey;

    private static final Map<Integer, WebSocketVoTypeEnum> cache;


    static {
        cache = Arrays.stream(WebSocketVoTypeEnum.values()).collect(Collectors.toMap(WebSocketVoTypeEnum::getType, Function.identity()));
    }

    public static WebSocketVoTypeEnum of(Integer type) {
        return cache.get(type);
    }

    public static String ofRouterKey(Integer type) {
        for (WebSocketVoTypeEnum tmp : values()) {
            if (tmp.getType().equals(type)) {
                return tmp.getRouterKey();
            }
        }
        return null;
    }
}
